package com.cvicse.jrcompetition01.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cvicse.jrcompetition01.common.exception.BusinessException;
import com.cvicse.jrcompetition01.common.exception.BusinessExceptionEnum;
import com.cvicse.jrcompetition01.controller.form.InsertMechanismForm;
import com.cvicse.jrcompetition01.entity.Mechanism;
import com.cvicse.jrcompetition01.entity.dto.MechanismDto;
import com.cvicse.jrcompetition01.mapper.MechanismDepartmentMapper;
import com.cvicse.jrcompetition01.mapper.MechanismMapper;
import com.cvicse.jrcompetition01.service.MechanismService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cvicse.jrcompetition01.utils.AutoGenerateCodeUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 作者:kissshotwang@163.com
 * @since 2022-07-21
 */
@Service
public class MechanismServiceImpl extends ServiceImpl<MechanismMapper, Mechanism> implements MechanismService {

    @Resource
    MechanismMapper mechanismMapper;

    @Resource
    AutoGenerateCodeUtil autoGenerateCodeUtil;


    @Override
    public Integer add(InsertMechanismForm insertMechanismForm) {
        //        Mechanism mechanism = JSONUtil.parse(insertMechanismForm).toBean(Mechanism.class);
        /**MechanismType  是前端的复选框，值是字符串的形式。接受必须用字符串形式
         * 1.前端处理成字符串不合适
         * 2.String []接受再转化  也是无法实现，因为实体已经是String[]类型了，无法转化成String储存
         * 3.json
         * 4.form接受体
         * */
        Mechanism mechanism = new Mechanism();
        List<Mechanism> mechanismList = mechanismMapper.selectList(new QueryWrapper<Mechanism>());
        //排序
        Collections.sort(mechanismList);

        int size = 1;
        //考虑函数复用
        if (mechanismList.size() != 0) {
            size = mechanismList.get(mechanismList.size() - 1).getId();
        }
        String uuid = "";
        if (null != autoGenerateCodeUtil.getMap().get("ZJ") && !autoGenerateCodeUtil.getMap().get("ZJ").equals("00000001")) {
            autoGenerateCodeUtil.getMap().put("ZJ", "00000001");
        }
        for (int i = 0; i < size+1; i++) {
            uuid = autoGenerateCodeUtil.getNum();
        }
        mechanism.setMechanismNumber(uuid);

        mechanism.setMechanismStatu("意向机构");
        mechanism.setHostDepartment("资金投行部");
        mechanism.setVisit(2);
        mechanism.setVisitedTime(3);
        mechanism.setCreated(LocalDate.now());
        mechanism.setBusinessAddress(insertMechanismForm.getBusinessAddress());
        mechanism.setLegalRepresentative(insertMechanismForm.getLegalRepresentative());
        mechanism.setMechanismName(insertMechanismForm.getMechanismName());
        mechanism.setMechanismType(Arrays.toString(insertMechanismForm.getMechanismType()));
        mechanism.setUsccl(insertMechanismForm.getUsccl());
        mechanism.setRegistereAddress(insertMechanismForm.getRegistereAddress());
        mechanism.setValidity(insertMechanismForm.getValidity());
        int insert = 0;
        try {
            insert = mechanismMapper.insert(mechanism);
        } catch (Exception e) {
            throw new BusinessException(BusinessExceptionEnum.MECHANISM_CREATE_ERRPR);
        }
        return insert;
    }

    @Override
    public MechanismDto info(Long id) {
        Mechanism mechanismServiceById = mechanismMapper.selectById(id);
        MechanismDto mechanismDto = new MechanismDto();
        String replace = mechanismServiceById.getMechanismType().replace(" ", "");
        int length = replace.length()-1;
        String[] split = replace.substring(1, length).split(",");
        mechanismDto.setMechanismType(split);
        mechanismDto.setRegistereAddress(mechanismServiceById.getRegistereAddress());
        mechanismDto.setBusinessAddress(mechanismServiceById.getBusinessAddress());
        mechanismDto.setCreated(mechanismServiceById.getCreated());
        mechanismDto.setHostDepartment(mechanismServiceById.getHostDepartment());
        mechanismDto.setLegalRepresentative(mechanismServiceById.getLegalRepresentative());
        mechanismDto.setMechanismName(mechanismServiceById.getMechanismName());
        mechanismDto.setMechanismNumber(mechanismServiceById.getMechanismNumber());
        mechanismDto.setMechanismStatu(mechanismServiceById.getMechanismStatu());
        mechanismDto.setUsccl(mechanismServiceById.getUsccl());
        mechanismDto.setValidity(mechanismServiceById.getValidity());
        mechanismDto.setVisit(mechanismServiceById.getVisit());
        mechanismDto.setVisitedTime(mechanismServiceById.getVisitedTime());
        mechanismDto.setId(mechanismServiceById.getId());
        return mechanismDto;
    }


    @Override
    public Integer update(MechanismDto mechanismDto) {
        Mechanism mechanism = new Mechanism();
        mechanism.setId(mechanismDto.getId());
        mechanism.setValidity(mechanismDto.getValidity());
        mechanism.setRegistereAddress(mechanismDto.getRegistereAddress());
        mechanism.setUsccl(mechanismDto.getUsccl());
        mechanism.setMechanismType(Arrays.toString(mechanismDto.getMechanismType()));
        mechanism.setLegalRepresentative(mechanismDto.getLegalRepresentative());
        mechanism.setMechanismName(mechanismDto.getMechanismName());
        mechanism.setBusinessAddress(mechanismDto.getBusinessAddress());
        mechanism.setCreated(mechanismDto.getCreated());
        mechanism.setVisitedTime(mechanismDto.getVisitedTime());
        mechanism.setHostDepartment(mechanismDto.getHostDepartment());
        mechanism.setMechanismStatu(mechanismDto.getMechanismStatu());
        mechanism.setMechanismNumber(mechanismDto.getMechanismNumber());
        int i = mechanismMapper.updateById(mechanism);
        return i;
    }
}
